package og.example.views.student;
import og.example.controller.StudentController;
import og.example.views.MainFrame;
import og.example.dao.GroupDataProvider;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentFrame extends JFrame {
    private JTextField studentIdField;
    private JTextField studentNameField;
    private JTextField groupNameField;

    public StudentFrame() {
        setTitle("添加学生");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel(new GridLayout(3, 2));
        JLabel studentIdLabel = new JLabel("学号：");
        studentIdField = new JTextField(15);
        JLabel studentNameLabel = new JLabel("姓名：");
        studentNameField = new JTextField(15);
        JLabel groupNameLabel = new JLabel("小组名称：");
        groupNameField = new JTextField(15);
        JButton addButton = new JButton("添加");
        JButton cancelButton = new JButton("返回");

        mainPanel.add(studentIdLabel);
        mainPanel.add(studentIdField);
        mainPanel.add(studentNameLabel);
        mainPanel.add(studentNameField);
        mainPanel.add(groupNameLabel);
        mainPanel.add(groupNameField);
        mainPanel.add(addButton);
        mainPanel.add(cancelButton);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Long studentId = Long.parseLong(studentIdField.getText());
                String studentName = studentNameField.getText();
                String groupName = groupNameField.getText();
                if (studentId == null || studentName.isEmpty() || groupName.isEmpty()) {
                    JOptionPane.showMessageDialog(StudentFrame.this, "所有字段都是必填项！");
                } else {
                    StudentController studentController = new StudentController();
                    boolean isAdded = studentController.studentAdd(studentId, studentName, groupName);
                    if (isAdded) {
                        JOptionPane.showMessageDialog(StudentFrame.this, "添加成功！");
                    } else {
                        JOptionPane.showMessageDialog(StudentFrame.this, "添加成功！");
                    }
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭当前窗口
                new MainFrame(); // 打开主界面
            }
        });
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Long studentId = Long.parseLong(studentIdField.getText());
                String studentName = studentNameField.getText();
                String groupName = groupNameField.getText();
                if (studentId == null || studentName.isEmpty() || groupName.isEmpty()) {
                    JOptionPane.showMessageDialog(StudentFrame.this, "所有字段都是必填项！");
                } else {
                    StudentController studentController = new StudentController();
                    boolean isAdded = studentController.studentAdd(studentId, studentName, groupName);
                    if (isAdded) {
                        JOptionPane.showMessageDialog(StudentFrame.this, "添加成功！");
                        // 更新小组的学生数量
                        GroupDataProvider.incrementStudentCountForGroup(groupName);
                    } else {
                        JOptionPane.showMessageDialog(StudentFrame.this, "添加失败，学号可能已存在！");
                    }
                    dispose();
                    new MainFrame();
                }
            }
        });

        setContentPane(mainPanel);
        setVisible(true);
    }
}